package com.fullsemi.mitgeneralservice.controller;

import com.fullsemi.mitgeneralservice.common.domain.AjaxResult;
import com.fullsemi.mitgeneralservice.common.page.TableDataInfo;
import com.fullsemi.mitgeneralservice.common.utils.SecurityUtils;
import com.fullsemi.mitgeneralservice.common.utils.StringUtils;
import com.fullsemi.mitgeneralservice.entity.User;
import com.fullsemi.mitgeneralservice.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Api(tags="用户管理")
@RestController
@RequestMapping("/user")
public class UserController extends BaseController{
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    IUserService userService;

    @ApiOperation("用户列表")
    @GetMapping("/list")
    public TableDataInfo list(User user, HttpServletRequest request, HttpServletResponse response){
//        Cookie cookie = new Cookie("name", "stephen");
//        response.addCookie(cookie);
        request.getSession().setAttribute("sessionid","88888888");
        logger.info("get request from: " + request.getRemoteAddr());
        startPage();
        List<User> list = userService.selectUserList(user);
        return getDataTable(list);

    }

    @ApiOperation("用户详情")
    @GetMapping(value = { "/{id}" })
    public AjaxResult getInfo(@PathVariable(value = "id", required = false) Integer id){
        AjaxResult ajax = AjaxResult.success();
        if(StringUtils.isNotNull(id)){
            ajax.put(AjaxResult.DATA_TAG,userService.selectUserById(id));
        }
        return ajax;
    }

    /**
     * 新增用户
     */
    @ApiOperation("新增用户")
    @PostMapping
    public AjaxResult add(@RequestBody User user)
    {
        if (userService.checkEmployNoExist(user)){
            return AjaxResult.error("新增用户'" + user.getUserName() + "'失败，此工号已存在");
        }
        if (StringUtils.isNotEmpty(user.getEmail()) && (userService.checkEmailExist(user))){
            return AjaxResult.error("新增用户'" + user.getUserName() + "'失败，邮箱账号已存在");
        }
        user.setPasswd(SecurityUtils.encryptPassword(user.getPasswd()));
        return toAjax(userService.insertUser(user));
    }

    /**
     * 修改用户
     */
    @ApiOperation("修改用户")
    @PutMapping()
    public AjaxResult edit(@RequestBody User user)
    {
        if (userService.checkEmployNoExist(user)){
            return AjaxResult.error("新增用户'" + user.getUserName() + "'失败，此工号已存在");
        }
        if (StringUtils.isNotEmpty(user.getEmail()) && (userService.checkEmailExist(user))){
            return AjaxResult.error("新增用户'" + user.getUserName() + "'失败，邮箱账号已存在");
        }
        return toAjax(userService.updateUser(user));
    }

    /**
     * 删除用户
     */
    @ApiOperation("删除用户")
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return toAjax(userService.deleteUserByIds(ids));
    }
}
